package com.huatai.dataservice.fire.util;

import cn.hutool.core.date.DateTime;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gaowengui
 * @describe 工具包
 */
public class FireUtils {
	/**
	 * @param examType
	 * @param name
	 * @param examDate
	 * @param score
	 * @return
	 * @author gaowengui
	 * @describe
	 */
	public static Map<String, Object> createExamMap(String examType, String name, Date examDate, double score) {
		return new HashMap<String, Object>() {{
			put("ExamType", examType);
			put("Name", name);
			put("ExamDate", dateTimeFormate(examDate));
			put("Score", score);
		}};
	}

	/**
	 * @param carType
	 * @param count
	 * @return
	 * @author gaowengui
	 * @describe 获取消防车类型map集合
	 */
	public static Map<String, Object> createCarMap(String carType, int count) {
		return new HashMap<String, Object>() {{
			put("carType", carType);
			put("count", count);
		}};
	}

	/**
	 * @param redGrade
	 * @param depResult
	 * @param amountResult
	 * @return
	 * @author gaowengui
	 * @describe 业务能力指数计算
	 */
	public static double businessCapability(double redGrade, double depResult, double amountResult) {
		double businessIndex = redGrade * 0.15 + depResult * 0.45 + amountResult * 0.4;
		return Math.round(businessIndex * 10.0) / 10.0;
	}

	/**
	 * @param overNumber
	 * @param deptAllOverNumber
	 * @return
	 * @author gaowengui
	 * @describe 响应速度指数计算 工作压力指数计算
	 */
	public static double workPressure(double overNumber, double deptAllOverNumber) {
		if (deptAllOverNumber == 0) return 0.0;
		double work = (overNumber / deptAllOverNumber) * 100;
		return Math.round(work * 10.0) / 10.0;
	}

	/**
	 * @describe 个人的效能指数
	 * @param businessCapability
	 * @param workPressure
	 * @param response
	 * @param redGrade
	 * @param depResult
	 * @param amountResult
	 * @return
	 */

	public static double personalEffectiveness(double businessCapability,double workPressure,double response,
											   double redGrade,double depResult,double amountResult) {
		double personalEffectiveness = ((businessCapability + workPressure + response)/3 + redGrade +depResult+ amountResult)/3;
		return Math.round(personalEffectiveness * 10.0) / 10.0;
	}
	/**
	 * @param sourse
	 * @param operateTime
	 * @return
	 * @author gaowengui
	 * @describe 工作压力表
	 */
	public static Map<String, Object> pressureList(String sourse, Date operateTime) {
		return new HashMap<String, Object>() {{
			put("Sourse", sourse);
			put("OperateTime",dateTimeFormate(operateTime));
		}};
	}

	/**
	 * @param typeName
	 * @param endTime
	 * @param finishTime
	 * @return
	 * @author gaowengui
	 * @describe 响应速度表
	 */
	public static Map<String, Object> responseMap(String typeName, Date endTime, Date finishTime) {
		return new HashMap<String, Object>() {{
			put("TypeName", typeName);
			put("EndTime", dateTimeFormate(endTime));
			put("FinishTime", dateTimeFormate(finishTime));
		}};
	}


	/**
	 * @param carType
	 * @param count
	 * @return
	 * @author gaowengui
	 * 获取消防车类型map集合
	 */
	public  static Map<String, Object> createCarMaps(String carType, int count) {
		return new HashMap<String, Object>() {{
			put("name", carType);
			put("value", count);
		}};
	}

	/**
	 * 计算时间差
	 * @param actualFinishTime
	 * @param requiredFinishTime
	 * @return
	 */
	public static double calculateHourDifference(Date actualFinishTime, Date requiredFinishTime) {
		// 空值检查
		if (actualFinishTime == null || requiredFinishTime == null) {
			throw new IllegalArgumentException("时间参数不能为null");
		}
		long diffInMillis = Math.abs(actualFinishTime.getTime() - requiredFinishTime.getTime());
		double diffInHours = (double) diffInMillis / (1000 * 60 * 60);
		return Math.round(diffInHours * 100.0) / 100.0;
	}

	/**
	 * @param carType
	 * @param count
	 * @return
	 * @author gaowengui
	 * 逾期
	 */
	public  static Map<String, Object> beOverdue(String carType, int count) {
		return new HashMap<String, Object>() {{
			put("periodName", carType);
			put("count", count);
		}};
	}

	/**
	 * @author gaowengui
	 * 异常风险得分
	 * @param number
	 * @return
	 */
	public static int abnormalRiskScore(int number){
		return 100-number;
	}

	/**
	 * @author gaowengui
	 * 时效风险得分
	 * @param number
	 * @return
	 */
	public static int outTimeRiskScore(int number){
		return 100-number*5;
	}

	/**
	 * 合规风险得分
	 * @author gaowengui
	 * @param number
	 * @return
	 */
	public static int complianceRisk(int number){
		return 100-number*10;
	}

	/**
	 * @author gaowengui
	 * 风险评分
	 * @param abnormalRiskScore 时效风险得分
	 * @param outTimeRiskScore 时效风险得分
	 * @param complianceRiskScore 合规风险得分
	 * @return
	 */
	public static double riskScore(int abnormalRiskScore,int outTimeRiskScore,int complianceRiskScore){
		return abnormalRiskScore*0.25+outTimeRiskScore*0.4+complianceRiskScore*0.35;
	}

	/**
	 * @author gaowengui
	 * 由“重点部位名称”、“告警小类”、“告警数量”组合起来的文字，格式：
	 *  在 {{重点部位名称}}，发现{{告警数量}}处{{告警小类}}
	 * @param unitName
	 * @param count
	 * @param subClass
	 * @return
	 */
	public static String getName(String unitName,int count,String subClass){
		return String.format("在%s，发现%s处%s", unitName, count, subClass);
	}

	/**
	 * @author gaowengui
	 * 手机号脱敏
	 * @param phone
	 * @return
	 */
	public static String phoneDesensitization(String phone){
		String regex = "^1[3-9]\\d{9}$";
		if (phone.matches(regex)) {
			String desensitized = phone.replaceAll(
					"(1[3-9]\\d{2})\\d{4}(\\d{4})",
					"$1****$2"
			);
			return desensitized;
		}
		return null;
	}

	/**
	 * @author gaowengui
	 * 时间格式化
	 * @param originalTime
	 * @return
	 */
	public static String dateTimeFormate(Date originalTime){
		DateTimeFormatter FORMATTER =
				DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		Instant instant = originalTime.toInstant();
		ZonedDateTime utcTime = instant.atZone(ZoneId.of("UTC"));
		ZonedDateTime targetTime = utcTime.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));
		return targetTime.format(FORMATTER);
	}


	/**
	 * @return a
	 * @Author gaowengui
	 * @Descrption 计算平均数 保留两位小数
	 * @Date 15:20 2025/6/1
	 * @Param
	 **/
	public static double calculateRatio(double score, int staff) {
		if (staff == 0) {
			throw new IllegalArgumentException("Staff count cannot be zero.");
		}
		BigDecimal scoreBD = BigDecimal.valueOf(score);
		BigDecimal staffBD = BigDecimal.valueOf(staff);
		BigDecimal ratio = scoreBD.divide(staffBD, 2, RoundingMode.HALF_UP);
		return ratio.doubleValue();
	}
}
